/*******************************************************************************
 ** Name: common.h                                                            **
 ** Description: ATmega328p (or generic) common stuff                         **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License                             **
 **                                                                           **
 ** The contents of this file are subject to the terms of the                 **
 ** Common Development and Distribution License, Version 1.0 only             **
 ** (the "License").  You may not use this file except in compliance          **
 ** with the License.                                                         **
 **                                                                           **
 ** You can find a copy of the license in the LICENSE file within             **
 ** this distribution or at www.opendesignstrategies.org/license.             **
 ** Software distributed under the License is distributed on an "AS IS"       **
 ** basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.           **
 ** See the License for the specific language governing permissions           **
 ** and limitations under the License.                                        **
 **                                                                           **
 ** When distributing Covered Code, include this CDDL header in each          **
 ** file and include the License file at $HOME_DIR/LICENSE.                   **
 ** If applicable, add the following below this header, with the indicated    **
 ** fields enclosed by brackets "[]" replaced with your own identifying       **
 ** information: Portions Copyright [yyyy] [name of copyright owner]          **
 **                                                                           **
 ** Copyright (c) 2009-2012 Open Design Strategies, LLC                       **
 ** All rights reserved.                                                      **
 ******************************************************************************/
// $HeadURL$
// Modified $Date$ by $Author$
// $Id$

#ifndef COMMON_H_
#define COMMON_H_

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <stdint.h>

#define	PRESCALER	1024
#define CHECK_TIME	((F_CPU/PRESCALER)/4)	// 250ms
#define StartTimer()	(TCCR1B |= (1<<CS10); TCCR1B |= (1<<CS12))
#define StopTimer()		(TCCR1B &= ~(1<<CS10); TCCR1B &= ~(1<<CS12))
#define ClearTimer()	(TCNT1 = 0)

#define BEGIN_CRITICAL_SECTION	(asm volatile("ldi r16,SREG\n\tcli\n\t"::);)
#define END_CRITICAL_SECTION	(asm volatile("out SREG,r16\n\tsei\n\t"::);)

#define BSET(ADDRESS,BIT) (ADDRESS |= (unsigned char)(1<<BIT))
#define BCLR(ADDRESS,BIT) (ADDRESS &= (unsigned char)~(1<<BIT))
#define BTOG(ADDRESS,BIT) (ADDRESS ^= (unsigned char)(1<<BIT))
#define BCHK(ADDRESS,BIT) (ADDRESS &  (unsigned char)(1<<BIT))

#define BMSET(x,y) (x |= (y))
#define BMCLR(x,y) (x &= (~y))
#define BMTOG(x,y) (x ^= (y))
#define BMCHK(x,y) (x & (y))

#ifdef USE_CPP_GUARDS
	#ifdef __cplusplus
	extern "C" {
	#endif
		void __cxa_pure_virtual(void);
		#if !defined(NO_THREADSAFE_STATICS)
			int __cxa_guard_acquire(__guard *);
			void __cxa_guard_release(__guard *);
			void __cxa_guard_abort(__guard*);
		#endif  /* !NO_THREADSAFE_STATICS */
		#if defined(USE_DYNAMIC_MEM)
			void * operator new(size_t size);		// new replacement
			void * operator new(size_t, void*);		// placement new
			void * operator new[] (size_t size);
			void operator delete(void * ptr);
			void operator delete(void * p, void * const b);	// placement delete..hmmm
			void operator delete[] (void * ptr);
		#endif
	#ifdef __cplusplus
	}
	#endif

#endif

#endif /* COMMON_H_ */